import constant from "./constant";
import { TUIConversationService } from "@tencentcloud/chat-uikit-engine";
import TencentCloudChat from '@tencentcloud/chat';
import { TUIChatKit, genTestUserSig } from "@/TUIKit";
import { TUILogin } from "@tencentcloud/tui-core";


function init(){
	uni.$chat_SDKAppID = constant.imApppId;
	uni.$chat_secretKey = constant.imSecretKey;
	uni.$TUIKit = TencentCloudChat.create({
		SDKAppID: uni.$chat_SDKAppID
	});
	//离线推送
	// uni.$TUIOfflinePush = uni.requireNativePlugin("TencentCloud-TUIOfflinePush");
	// console.warn(uni.$TUIOfflinePush, "TUIOfflinePush ｜ ok");
}


function login() {
	const txCode = uni.getStorageSync("userTxCode");
	const token = uni.getStorageSync("token");
	if (txCode && token) {
		uni.$chat_userID = txCode + '';
		const config = {
			userID: uni.$chat_userID,
			SDKAppID: uni.$chat_SDKAppID,
			secretKey: uni.$chat_secretKey,
		}
		uni.$chat_userSig = genTestUserSig(config).userSig;
		TUIChatKit.init();

		const options = {
			SDKAppID: uni.$chat_SDKAppID,
			userID: uni.$chat_userID,
			userSig: uni.$chat_userSig,
			useUploadPlugin: true,
			framework: `vue2`,
			// TUIOfflinePush: uni.$TUIOfflinePush, // App 注册离线推送插件
			// offlinePushConfig: {
			// 	// huawei
			// 	huaweiBusinessID: '33207', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
			// 	// xiaomi
			// 	xiaomiBusinessID: '30110', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
			// 	xiaomiAppID: '2882303761520247850', // 小米开放平台分配的应用 APPID
			// 	xiaomiAppKey: '5562024796850', // 小米开放平台分配的应用 APPKEY
			// 	// vivo
			// 	vivoBusinessID: '33244', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
			// 	// oppo
			// 	oppoBusinessID: '33245', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
			// 	oppoAppKey: '076df83f6ee946b591916e2dd4bb9833', // oppo 开放平台分配的应用 AppKey
			// 	oppoAppSecret: 'cdf8f66a8f3e48f3951d1a1e28f9cd17', // oppo 开放平台分配的应用 AppSecret
			// 	// honor
			// 	honorBusinessID: '', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
			// 	// ios
			// 	iosBusinessID: '', // 在腾讯云控制台上传第三方推送证书后分配的证书 ID
			// },
		};
		//IM登录
		TUILogin.login(options).then(res => {
			console.log("IM登录成功：" + JSON.stringify(res));
			initUnReadMessage();
		}).catch(error => {
			console.log('IM登录失败:' + JSON.stringify(error));
		});
	}
}


// 打开通知栏
function initOfflinePushListener(){
	// uni.$TUIOfflinePush.setOfflinePushListener((data) => {
	//     console.log('打开离线消息', data);
	//     uni.switchTab({
	//         url: "/pages/tab/message/message",
	//     });
	// });
}


//跳转到聊天
function toChat(txCode) {
	// getApp().globalData.chatUserId = this.user.id;
	getApp().globalData.chatTxCode = txCode;
	const conversationID = `C2C` + txCode;
	uni.$TUIKit.setMessageRead({
		conversationID: conversationID
	}).then(res => {
		const count = uni.$TUIKit.getTotalUnreadMessageCount();
		if (count > 0) {
			uni.setTabBarBadge({
				index: constant.messageIndex,
				text: '' + count
			})
		} else {
			uni.removeTabBarBadge({
				index: constant.messageIndex,
			})
		}
	});
	TUIConversationService.switchConversation(conversationID);

	uni.navigateTo({
		url: `/TUIKit/components/TUIChat/index?conversationID=${conversationID}`,
	});
}


function initUnReadMessage() {
	//初始化未读消息
	setTimeout(()=>{
		uni.$TUIKit.getConversationList().then(res => {
			const unReadMessageCount = uni.$TUIKit.getTotalUnreadMessageCount();
			setUnReadMessageCount(unReadMessageCount);
		}).catch(error => {});
	}, 1000)
	
	//监听未读消息数
	uni.$TUIKit.on(TencentCloudChat.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED, event => {
		setUnReadMessageCount(event.data);
	});
}

//设置未读数量
function setUnReadMessageCount(count) {
	if (count > 0) {
		uni.setTabBarBadge({
			index: constant.messageIndex,
			text: '' + count
		})
	} else {
		uni.removeTabBarBadge({
			index: constant.messageIndex
		})
	}
}

export default {
	init,
	login,
	initOfflinePushListener,
	toChat
}